package controller

import (
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

type Account struct{}

func (a *Account) AddAccount(c *gin.Context) {

	var account *request.Account

	c.ShouldBindJSON(&account)

	// 获取token
	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage("授权失败", c)
	} else {
		if err := services.AccountService.AddAccount(claims.BaseClaims, account); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.Ok(c)
		}
	}

}

func (a *Account) EditAccount(c *gin.Context) {

	var account *request.Account

	c.ShouldBindJSON(&account)

	if err := services.AccountService.EditAccount(account); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}

}
func (a *Account) GetAccountInfo(c *gin.Context) {
	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage("授权失败", c)
	} else {
		if account, err := services.AccountService.GetAccountInfo(claims.BaseClaims); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.OkWithData(account, c)
		}
	}
}

func (a *Account) GetAccountList(c *gin.Context) {

	var paginate request.AccountPage

	c.ShouldBind(&paginate) //获取请求参数

	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage("获取失败", c)
	} else {
		if accounts, err := services.AccountService.GetAccountList(claims.BaseClaims, &paginate); err != nil {
			utils.FailWithMessage(err.Error(), c)
			return
		} else {
			utils.OkWithData(accounts, c)
		}
	}

}

// 根据账号获取地区
func (a *Account) GetAreaByAccount(c *gin.Context) {

	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage("授权失败", c)
	} else {
		if regions, err := services.AccountService.GetAreaByAccount(claims.BaseClaims); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.OkWithData(regions, c)
		}
	}
}

// 设置账号区域
func (a *Account) SetAccountRegion(c *gin.Context) {

	var ids request.SetRegion

	c.ShouldBindJSON(&ids)

	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage("授权失败", c)
	} else {
		if err := services.AccountService.SetAccountRegion(claims.BaseClaims, ids); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.Ok(c)
		}
	}

}

// 根据账户获取子角色
func (a *Account) GetRoleByAccount(c *gin.Context) {
	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage("授权失败", c)
	} else {
		if roles, err := services.AccountService.GetRoleByAccount(claims.BaseClaims); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.OkWithData(roles, c)
		}
	}
}

// 获取当前可以设置的区域
func (a *Account) GetCanSetRegionByAccount(c *gin.Context) {
	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage("授权失败", c)
	} else {
		if roles, err := services.AccountService.GetCanSetRegionByAccount(claims.BaseClaims); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.OkWithData(roles, c)
		}
	}
}

func (a *Account) Delete(c *gin.Context) {
	var ids *request.IDS

	c.ShouldBindJSON(&ids)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.AccountService.Delete(ids.IDS, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (a *Account) SetStatus(c *gin.Context) {
	var data *request.SetStatus

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.AccountService.SetStatus(data, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

var AccountController = new(Account)
